package com.exthirteen.textgamescript.websocket;

import lombok.Getter;
import lombok.Setter;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.enums.Opcode;
import org.java_websocket.framing.Framedata;

import java.nio.ByteBuffer;
import java.util.Collection;

/**
 * <p>
 * 客户端POJO
 * </p>
 *
 * @author exthirteen
 * @since 2022/9/22
 */
public class Client {

    /**
     * 客户端唯一ID
     */
    @Getter
    private final String id;

    /**
     * socket客户端
     */
    @Getter
    private WebSocketClient socketClient;

    /**
     * 重新连接的函数
     */
    @Setter
    private Runnable reconnectRun;

    public Client(String id, WebSocketClient socketClient) {
        this.id = id;
        this.socketClient = socketClient;
    }

    public void send(String text){
        socketClient.send(text);
    }

    public void send(ByteBuffer bytes){
        socketClient.send(bytes);
    }

    public void send(byte[] bytes){
        socketClient.send(bytes);
    }

    public void sendFrame(Framedata framedata){
        socketClient.sendFrame(framedata);
    }

    public void sendFrame(Collection<Framedata> frames){
        socketClient.sendFrame(frames);
    }

    public void sendPing(){
        socketClient.sendPing();
    }

    public void sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin){
        socketClient.sendFragmentedFrame(op,buffer,fin);
    }

    void setSocketClient(WebSocketClient socketClient) {
        this.socketClient = socketClient;
    }

    public void reconnect(){
        reconnectRun.run();
    }
}
